package org.journal.dao;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.journal.entity.UserEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Repository
public class UserDAOImpl extends GenericDAOImpl<UserEntity, Integer> implements
		UserDAO {

	
	@Autowired
	SessionFactory sessionFactory;
	
	private static final Logger log = LoggerFactory
			.getLogger(UserDAOImpl.class);
	
	
	private static final String GET_USER_NAME_NAMED_QUERY = "UserEntity.getUserByName";

	public Integer createUser(UserEntity user) {
		return (Integer) save(user);

	}

	public UserEntity getUserById(Integer id) {

		return getEntityById(UserEntity.class, id);

	}

	@Transactional
	public UserEntity getUserByName(String username) {

		Query query = sessionFactory.getCurrentSession().getNamedQuery(
				GET_USER_NAME_NAMED_QUERY);
		query.setString("username", username);

		return searchUnique(query);

	}
}
